// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// j [author] rhyme_lph
// Q [email]  rhymelph@gmail.com
// g [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/entities/sub_cards_entity.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/list_item/pay_bill_item.dart';
import 'package:finance_app/src/widgets/select/pay_bill_item_radio.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/setting/subscription/sub_payment_method_manager_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/generated/a.dart';

@RouterMeta(paramName: "subPaymentMethodManagerPage")
class SubPaymentMethodManagerPage extends StatefulWidget {
  @override
  _SubPaymentMethodManagerPageState createState() =>
      _SubPaymentMethodManagerPageState();
}

class _SubPaymentMethodManagerPageState
    extends State<SubPaymentMethodManagerPage>
    with
        PagePresenter<SubPaymentMethodManagerPage,
            SubPaymentMethodManagerProvider> {
  @override
  PreferredSizeWidget get appBar =>
      getDefaultAppBar(title: S.current.manage_payment_method);

  @override
  Widget get buildEmptyWidget => buildWidget;

  @override
  Widget get buildWidget => ListView(
        padding:
            EdgeInsets.only(left: getDp(25), right: getDp(28), top: getDp(20)),
        children: [
          listBuilder<int>(
            selector: (_, __) => __.data.length,
            builder: (BuildContext context, int value, Widget child) => Column(
              children: List.generate(
                  value,
                  (index) => listBuilder<SubCardsEntity>(
                        selector: (_, __) => __.data[index],
                        builder: (BuildContext context, SubCardsEntity value,
                                Widget child) =>
                            Padding(
                          padding: EdgeInsets.symmetric(vertical: getDp(10)),
                          child: listBuilder<bool>(
                            selector: (_, __) => __.select == value,
                            builder: (BuildContext context, bool isSelect,
                                    Widget child) =>
                                PayBillItemRadio(
                              title: value.cardNumber,
                              isSelect: isSelect,
                              onDelete: () => provider.onDelete(value),
                              onTap: () => provider.onSelect(value),
                            ),
                          ),
                        ),
                      )),
            ),
          ),
          AppSpaces.h10,
          PayBillItem(
            assertPath: A.assetsImagesSettingCredit_card_1,
            title: S.current.add_credit_card,
            onTap: provider.onAddCreditCard,
          ),
        ],
      );
}
